
package com.x52im.rbchat.logic.chat_friend.gift;

import android.app.Activity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.x52im.rbchat.logic.chat_friend.gift.model.GiftInPackageMeta;
import com.x52im.rbchat.logic.chat_friend.gift.model.GiftsMeta;

import java.util.Observable;
import java.util.Observer;

/**
 * 礼品工具主UI实现根类.
 * 
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @since 2.5
 */
public abstract class GiftsToolsUIWraperRoot
{
	private Activity parentActivity = null;
	
	public GiftsToolsUIWraperRoot(Activity parentActivity)
	{
		this.parentActivity = parentActivity;
	}
	
	/**
	 * 为工具切换按钮添加处理处理对象.
	 * 
	 * @param bindedView
	 * @return
	 */
	protected OnCheckedChangeListener createOnCheckedChangeListener(final View bindedView)
	{
		return new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked)
			{
				if(isChecked)
					bindedView.setVisibility(View.VISIBLE);
				else
					bindedView.setVisibility(View.GONE);
				
				afterOnCheckedChange(bindedView, isChecked);
			}
		};
	}
	protected void afterOnCheckedChange(View bindedView, boolean isChecked)
	{
		//
	}
	
	/**
	 * 载入数据.
	 * 
	 * @param meta
	 * @param giftInPackageMeta
	 * @see #loadGiftsMetaDataAsync(GiftsMeta)
	 * @see #loadGiftInPackageMetaDataAsync(GiftInPackageMeta)
	 * @see #loadScoreDataAsync()
	 */
	public void loadAllDataAsync(GiftsMeta meta, GiftInPackageMeta giftInPackageMeta)
	{
		loadGiftsMetaDataAsync(meta);
		loadGiftInPackageMetaDataAsync(giftInPackageMeta);
		loadScoreDataAsync();
	}
	public void loadGiftsMetaDataAsync(GiftsMeta meta)
	{
		// 载入礼品数据
		if(meta != null)
			new GiftsProvider.LoadGiftsMetaAsyncTask(parentActivity).execute(meta, new Observer(){
				@Override
				public void update(Observable observable, Object data)
				{
					//** 把数据刷新到UI上
					onGiftsMetaLoaded((GiftsMeta)data);
				}
			});
	}
	public void loadGiftInPackageMetaDataAsync(GiftInPackageMeta giftInPackageMeta)
	{
		// 载入礼品包数据
		if(giftInPackageMeta != null)
			new GiftsProvider.LoadGiftInPackageMetaAsyncTask(parentActivity).execute(giftInPackageMeta, new Observer(){
				@Override
				public void update(Observable observable, Object data)
				{
					//** 把数据刷新到UI上
					onGiftInPackageMetaLoaded((GiftInPackageMeta)data);
				}
			});
	}
	public void loadScoreDataAsync()
	{
		// 载入当前积分
		new GiftsProvider.QueryScoreAsyncTask(parentActivity
				, new Observer(){
			@Override
			public void update(Observable observable, Object data)
			{
				int score = (Integer)data;
				//** 把数据刷新到UI上
				onScoreLoaded(score);
			}
		})
		.execute();
	}
	
	/**
	 * 数据载入完成后要调用的主法.
	 * 
	 * <p>
	 * 本方法中通常实现将载入完成的角色数据刷新到ui显示.
	 * 
	 * @param senceMeta 载入完成时所取到的角色数据对象，为null时表示数据载入失败
	 */
	protected abstract void onGiftsMetaLoaded(GiftsMeta senceMeta);
	protected abstract void onGiftInPackageMetaLoaded(GiftInPackageMeta senceMeta);
	protected abstract void onScoreLoaded(int score);
	
	/**
	 * 本方法将在父Activity resume时被调用.
	 */
	public abstract void onParentResume();
	/**
	 * 本方法将在父Activity pause时被调用.
	 */
	public abstract void onParentPause();
	
	protected abstract View findViewById(int resId);
	protected abstract void closeUI();
}
